﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sistema.Dominio;
using Sistema.IDAO;
using NHibernate;

namespace Sistema.DAO.Nhibernate.Helper
{
    public abstract class BaseDAONHibernate<T> : IObjDados<T> where T : Entidade
    {
        protected ISession _session = NhibernateSession.GetCurrentSession();

        public virtual void Salvar(T dados)
        {
            try
            {
                ITransaction operacao = _session.BeginTransaction();
                _session.Save(typeof(T).ToString(), dados);
                operacao.Commit();
            }
            catch (Exception ex)
            {
                throw new NHibernateException(String.Format(Properties.Resources.NhibernateExceptionMensagemErro, "BaseDAONhibernate.Salvar()", ex.Message),
                    String.Format(Properties.Resources.NHibernateExceptionMensagemAoCliente, "Salvar", dados.ToString()), ex);
            }
        }

        public virtual void Alterar(T dados)
        {
            try
            {
                ITransaction operacao = _session.BeginTransaction();
                _session.SaveOrUpdate(dados);
                operacao.Commit();
            }
            catch (Exception ex)
            {

                throw new NHibernateException(String.Format(Properties.Resources.NhibernateExceptionMensagemErro, "BaseDAONhibernate.Alterar()", ex.Message),
                    String.Format(Properties.Resources.NHibernateExceptionMensagemAoCliente, "Alterar", dados.ToString()), ex);
            }
        }
        public virtual void Excluir(T dados)
        {
            try
            {
                ITransaction operacao = _session.BeginTransaction();
                _session.Delete(dados);
                operacao.Commit();
            }
            catch (Exception ex)
            {
                throw new NHibernateException(String.Format(Properties.Resources.NhibernateExceptionMensagemErro, "BaseDAONhibernate.Excluir()", ex.Message),
                    String.Format(Properties.Resources.NHibernateExceptionMensagemAoCliente, "Excluir", dados.ToString()), ex);
            }
        }

        public virtual T Buscar(Int32 id)
        {
            try
            {
                return _session.Get<T>(id);
            }
            catch (Exception ex)
            {
                throw new NHibernateException(String.Format(Properties.Resources.NhibernateExceptionMensagemErro, "BaseDAONHibernate.Buscar(String)", ex.Message),
                    String.Format(Properties.Resources.NHibernateExceptionMensagemAoCliente, "Buscar", id));
            }
        }

        public virtual IList<T> Listar()
        {
            try
            {
                return _session.CreateCriteria<T>().List<T>();
            }
            catch (Exception ex)
            {
                throw new NHibernateException(String.Format(Properties.Resources.NhibernateExceptionMensagemErro, "BaseDAONHibernate.Listar()", ex.Message),
                    String.Format(Properties.Resources.NHibernateExceptionMensagemAoCliente, "Listar", ""));
            }
        }

        public virtual IList<T> Listar(String parametro)
        {
            try
            {
                return _session.CreateCriteria<T>().AddOrder(NHibernate.Criterion.Order.Desc(NHibernate.Criterion.Projections.Cast(NHibernateUtil.Int32,NHibernate.Criterion.Projections.Property(parametro)))).List<T>();
            }
            catch (Exception ex)
            {
                throw new NHibernateException(String.Format(Properties.Resources.NhibernateExceptionMensagemErro, "BaseDAONHibernate.Listar()", ex.Message),
                    String.Format(Properties.Resources.NHibernateExceptionMensagemAoCliente, "Listar", ""));
            }
        }
    }
}
